package com.prawn.ift_baseconfig.service.impl;


import com.prawn.ift_baseconfig.dao.WarningConfigDao;
import com.prawn.ift_baseconfig.pojo.NetworkType;
import com.prawn.ift_baseconfig.pojo.WarningConfig;
import com.prawn.ift_baseconfig.service.WarningConfigService;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.NullAttributeUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/25
 * @DESC： 预警阈值配置信息服务实现
 */
@Service
public class WarningConfigServiceImpl implements WarningConfigService {

    @Autowired
    private WarningConfigDao warningConfigDao;

    /**
     * 添加预警阈值配置信息
     *
     * @param warningConfig
     * @return Result
     */
    @Override
    public Result addWarningConfig(WarningConfig warningConfig) {
        warningConfig.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        warningConfig.setCreateDate(new Date());
        warningConfig.setUpdateDate(new Date());
        if (warningConfigDao.save(warningConfig) != null) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 删除预警阈值配置信息
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteWarningConfig(String id) {
        warningConfigDao.deleteById(id);
        return Result.success();

    }

    /**
     * 编辑预警阈值配置信息
     *
     * @param warningConfig
     * @return Result
     */
    @Override
    public Result editWarningConfig(WarningConfig warningConfig) {
        warningConfig.setUpdateDate(new Date());


        WarningConfig oldWarningConfig =warningConfigDao.findById(warningConfig.getId()).get();
        BeanUtils.copyProperties(warningConfig,oldWarningConfig, NullAttributeUtils.getNullPropertyNames(warningConfig));



        warningConfigDao.save(oldWarningConfig);
            return Result.success();

    }

    /**
     * 查询预警阈值配置信息
     *
     * @param id
     * @return WarningConfig
     */
    @Override
    public WarningConfig getWarningConfigById(String id) {
        return warningConfigDao.findById(id).get();
    }

    /**
     * 查询预警阈值配置信息列表
     *
     * @return Result
     */
    @Override
    public Result getWarningConfigList() {

        List<WarningConfig> warningConfigList = warningConfigDao.findAll();
        return Result.success().add(warningConfigList);
    }

    /**
     * 通过场景id查询预警阈值配置信息列表
     *
     * @return Result
     */
    @Override
    public Result getWarningConfigListByScenery(String sceneId) {
        WarningConfig warningConfig=new WarningConfig();
        warningConfig.setSceneId(sceneId);
        Example<WarningConfig> sExample = Example.of(warningConfig);
        List<WarningConfig> warningConfigList =warningConfigDao.findAll(sExample);

        return Result.success().add(warningConfigList);
    }

    /**
     * 通过场景id和采集项id查询预警阈值配置信息列表
     *
     * @param sceneId
     * @param channelId
     * @return Result
     */
    @Override
    public Result getWarningConfigListBySceneryAndChannel(String sceneId, String channelId) {

        WarningConfig warningConfig=new WarningConfig();
        warningConfig.setSceneId(sceneId);
        warningConfig.setCheckItemId(channelId);
        Example<WarningConfig> sExample = Example.of(warningConfig);
        List<WarningConfig> warningConfigList =warningConfigDao.findAll(sExample);
        return Result.success().add(warningConfigList);
    }

    /**
     * 分页查询预警阈值配置信息列表
     *
     * @return Result
     */
    @Override
    public Result getWarningConfigPage(int page, int rows) {
        PageRequest pageRequest =  PageRequest.of(page-1, rows);

        Page<WarningConfig> p = warningConfigDao.findAll(pageRequest);
        PageResult<WarningConfig> pageResult=new PageResult<>(p.getTotalElements(),p.getContent());

        return Result.success().add(pageResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchDelByIds(List<String> ids) {

        if(ids==null)
            return new Result(true,100,"ids为空，删除失败");
        for (String id:ids) {
            warningConfigDao.deleteById(id);
        }


        return new Result(true,200,"删除成功");
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<WarningConfig> createSpecification(Map searchMap) {

        return new Specification<WarningConfig>() {

            @Override
            public Predicate toPredicate(Root<WarningConfig> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) {
                    predicateList.add(cb.like(root.get("id").as(String.class), "%" + (String) searchMap.get("id") + "%"));
                }
                // 所属场景编号
                if (searchMap.get("sceneId") != null && !"".equals(searchMap.get("sceneId"))) {
                    predicateList.add(cb.like(root.get("sceneId").as(String.class), "%" + (String) searchMap.get("sceneId") + "%"));
                }
                // 检测项目
                if (searchMap.get("checkItemId") != null && !"".equals(searchMap.get("checkItemId"))) {
                    predicateList.add(cb.like(root.get("checkItemId").as(String.class), "%" + (String) searchMap.get("checkItemId") + "%"));
                }
                // 创建者
                if (searchMap.get("createBy") != null && !"".equals(searchMap.get("createBy"))) {
                    predicateList.add(cb.like(root.get("createBy").as(String.class), "%" + (String) searchMap.get("createBy") + "%"));
                }
                // 更新者
                if (searchMap.get("updateBy") != null && !"".equals(searchMap.get("updateBy"))) {
                    predicateList.add(cb.like(root.get("updateBy").as(String.class), "%" + (String) searchMap.get("updateBy") + "%"));
                }
                // 备注信息
                if (searchMap.get("remarks") != null && !"".equals(searchMap.get("remarks"))) {
                    predicateList.add(cb.like(root.get("remarks").as(String.class), "%" + (String) searchMap.get("remarks") + "%"));
                }
                // 删除标记
                if (searchMap.get("delFlag") != null && !"".equals(searchMap.get("delFlag"))) {
                    predicateList.add(cb.like(root.get("delFlag").as(String.class), "%" + (String) searchMap.get("delFlag") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

}
